package com.gm.commonlib.util.timeUtils;

import android.text.TextUtils;

import com.gm.commonlib.util.stringUtils.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class DateTimeUtils {
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static String LONG_DATETIME_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
    public static String SHORT_DATETIME_FORMAT_STRING = "yyyy-MM-dd";
    public static String YM = "yyyy年-MM月";
    public static String YMD = "yyyy-MM-dd";
    public static String HMD = "HH:mm:ss";
    public static String MDHMS = "MM-dd HH:mm:ss";
    public static String HMD2 = "MM-dd";
    private static Pattern a = Pattern.compile("^[1-9]\\d{3}-[01]?\\d-[0|1|2|3]?\\d$");
    private static SimpleDateFormat b;

    static {
        b = new SimpleDateFormat(SHORT_DATETIME_FORMAT_STRING);
    }

    public DateTimeUtils() {
    }

    public static String getFullDay(long time) {
        if (time > 0) {
            return formatDateTime(time, TimeFormat.FULL_DAY);
        }
        return "";
    }

    public static Date formatDateTime(String strDate, String formatter) {
        if (StringUtils.isEmpty(formatter)) {
            formatter = SHORT_DATETIME_FORMAT_STRING;
        }

        SimpleDateFormat formatter1 = new SimpleDateFormat(formatter);
        ParsePosition pos = new ParsePosition(0);
        return formatter1.parse(strDate, pos);
    }

    public static String getYMDHM(long time) {
        return formatDateTime(time, TimeFormat.Y_M_D_H_M);
    }

    public static String getYMD(long time) {
        return formatDateTime(time, TimeFormat.Y_M_D);
    }


public static String getWeekTime(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static long getTimeStringToLong(String time, String format) {
        if (time != null) {
            try {
                return new SimpleDateFormat(format).parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    public static String getLongDateTime() {
        Date currentTime = new Date();
        return (new SimpleDateFormat(LONG_DATETIME_FORMAT_STRING)).format(currentTime);
    }

//    public static String getLongDateTime(Date date) {
//        return (new SimpleDateFormat(LONG_DATETIME_FORMAT_STRING)).format(date);
//    }

    public static SimpleDateFormat createSimpleDateFormat(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        return simpleDateFormat;
    }

    public static SimpleDateFormat createSimpleDateFormat(String format, Locale locale) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, locale);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        return simpleDateFormat;
    }

    public static String buildYearMonthString(long timeMillis) {
        return parseTimeMillis2DateString(timeMillis, TimeFormat.FORMAT_STRING_YYYY_MM);
    }

//    public static String parseTimeMillis2DateString(long timeMillis) {
//        return parseTimeMillis2DateString(timeMillis, TimeFormat.FULL_DAY);
//    }

//    public static String parseTimeMillis2DateString(long d, String format) {
//        if (d <= 0) {
//            return "";
//        }
//        Date d1 = new Date(d);
//        return createSimpleDateFormat(format).format(d1);
//    }

//    public static Date parseTimeMillis2Date(long timeMillis, String format) {
//        return formatDateTime(formatDateTime(timeMillis, format), format);
//    }

//    public static Date formatDateTime(String strDate, String formatter) {
//        if (StringUtils.isEmpty(formatter)) {
//            formatter = TimeFormat.FORMAT_YYYYMMDD;
//        }
//
//        ParsePosition pos = new ParsePosition(0);
//        return createSimpleDateFormat(formatter).parse(strDate, pos);
//    }

    public static Date formatDateTime(String strDate, String formatter, Locale pos) {
        if (StringUtils.isEmpty(formatter)) {
            formatter = TimeFormat.FORMAT_YYYYMMDD;
        }

        ParsePosition pos1 = new ParsePosition(0);
        return createSimpleDateFormat(formatter, pos).parse(strDate, pos1);
    }

    public static boolean isToday(Date time){
        String inputTime=DateTimeUtils.formatDateTime(time,DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
        String today=DateTimeUtils.formatDateTime(System.currentTimeMillis(),DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
        if (inputTime.equals(today)){
            return true;
        }
        return false;
    }

    public static boolean isToday(String time){
        try{
            Date date=DateTimeUtils.formatDateTime(time,DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
            String inputTime=DateTimeUtils.formatDateTime(date,DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
            String today=DateTimeUtils.formatDateTime(System.currentTimeMillis(),DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
            if (inputTime.equals(today)){
                return true;
            }
            return false;
        }
        catch (Exception e){
            return false;
        }

    }

    public static String formatDateTime(Date date, String format) {
        if (date == null) {
            return "";
        }

        if (StringUtils.isEmpty(format)) {
            format = TimeFormat.FORMAT_YYYYMMDD;
        }

        return createSimpleDateFormat(format).format(date);
    }

    public static String formatDateTime(Date date, String format, Locale locale) {
        if (TextUtils.isEmpty(format)) {
            format = TimeFormat.FORMAT_YYYYMMDD;
        }

        return createSimpleDateFormat(format, locale).format(date);
    }

    public static String formatDateTime(String strDate, String time, String to_format) {
        new Date();
        try {
            Date time2 = createSimpleDateFormat(time, Locale.US).parse(strDate);
            strDate = createSimpleDateFormat(to_format).format(time2);
            return strDate;
        } catch (ParseException var3) {
            var3.printStackTrace();
            return strDate;
        }
    }

    public static String formatDateTime(long timeMillis, String format) {
        if (timeMillis <= 0) {
            return "";
        }
        return createSimpleDateFormat(format).format(Long.valueOf(timeMillis));
    }

    public static Date toLongDateTime(String strDate, String format) {
        ParsePosition pos = new ParsePosition(0);
        return createSimpleDateFormat(format).parse(strDate, pos);
    }

    public static String getLongDateTime(Date date) {
        return createSimpleDateFormat(TimeFormat.FULL_DAY).format(date);
    }

    public static String formatDuring(long mss) {
        long days = mss / 86400000L;
        long hours = mss % 86400000L / 3600000L;
        long minutes = mss % 3600000L / 60000L;
        long seconds = mss % 60000L / 1000L;
        return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
    }

    public static String parseDateTime2ReadableString(Date date) {
        return parseDateTime2ReadableString(date, TimeFormat.FULL_DAY);//"yyyy-MM-dd HH:mm:ss"
    }

    public static String parseDateTime2ReadableString(Date date, String format) {
        if (date == null) {
            return "";
        }
        long ms = Calendar.getInstance().getTime().getTime() - date.getTime();
        long seconds = ms / 1000L;
        if (seconds < 60L) {
            return "刚刚";
        } else {
            long minutes = seconds / 60L;
            if (minutes < 60L) {
                return minutes + "分钟前";
            } else {
                long hours = minutes / 60L;
                if (hours < 24) {
                    return hours + "小时前";
                } else {
                    return formatDateTime(date, format);
                }
            }
        }
    }

    public static String parseDateTime2ReadableString(long sb, String format) {
        long day = sb / 86400000L;
        long hour = (sb - day * 86400000L) / 3600000L;
        long minute = (sb - day * 86400000L - hour * 3600000L) / 60000L;
        long second = (sb - day * 86400000L - hour * 3600000L - minute * 60000L) / 1000L;
        long milliSecond = sb - day * 86400000L - hour * 3600000L - minute * 60000L - second * 1000L;
        StringBuffer sb1 = new StringBuffer();
        if (format.contains("dd") && day > 0L) {
            sb1.append(day + "天");
        }

        if (format.contains("hh") && hour > 0L) {
            sb1.append(hour + "小时");
        }

        if (format.contains("mm") && minute > 0L) {
            sb1.append(minute + "分");
        }

        if (format.contains("ss") && second > 0L) {
            sb1.append(second + "秒 ");
        }

        if (format.contains("SSS") && milliSecond > 0L) {
            sb1.append(milliSecond);
        }

        return sb1.toString();
    }

    public static boolean getDayDiffFromToday(Date dt, int type) {
        Date today;
        (today = new Date()).setHours(23);
        today.setMinutes(59);
        today.setSeconds(59);
        long diff = today.getTime() - dt.getTime();
        if (type == 0 && diff < 0L) {
            diff = 0L;
        } else {
            diff = Math.abs(diff);
        }

        long days = diff / 86400000L;
        return type == 0 && days == 0L ? true : (type == 1 && days > 0L && days <= 7L ? true : type == 2 && days >= 7L);
    }

    public static boolean isSameDay(long firstTime, long secondTime) {
        Date firstDate = new Date(firstTime);
        Date secondDate = new Date(secondTime);
        return firstDate.getYear() == secondDate.getYear()
                && firstDate.getMonth() == secondDate.getMonth()
                && firstDate.getDate() == secondDate.getDate();
    }

    public static boolean isSameMonth(long firstTime, long secondTime) {
        Date firstDate = new Date(firstTime);
        Date secondDate = new Date(secondTime);
        return firstDate.getYear() == secondDate.getYear()
                && firstDate.getMonth() == secondDate.getMonth();
    }

    public static boolean before(Date dateTime, Date date) {
        if (dateTime == null || date == null) {
            return false;
        }
        return dateTime.before(date);
    }

    public static boolean after(Date dateTime, Date date) {
        if (dateTime == null || date == null) {
            return false;
        }
        return dateTime.after(date);
    }

    public static boolean beforeToday(Date dateTime) {
        if (dateTime == null) {
            return false;
        }
        Date date = new Date();
        if (dateTime.getYear() < date.getYear()) {
            return true;
        } else if (dateTime.getYear() == date.getYear()) {
            if (dateTime.getMonth() < date.getMonth()) {
                return true;
            } else if (dateTime.getMonth() == date.getMonth()) {
                return dateTime.getDate() < date.getDate();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean afterToday(Date dateTime) {
        if (dateTime == null) {
            return false;
        }
        Date date = new Date();
        if (dateTime.getYear() > date.getYear()) {
            return true;
        } else if (dateTime.getYear() == date.getYear()) {
            if (dateTime.getMonth() > date.getMonth()) {
                return true;
            } else if (dateTime.getMonth() == date.getMonth()) {
                return dateTime.getDate() > date.getDate();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //TODO 比较年月日 是否 第一个小于第二个
    public static boolean isFirstLetterThanSecond(long firstTime, long secondTime) {
        return isFirstLetterOrEqualThanSecond(firstTime, secondTime, false);
    }

    public static boolean isFirstLetterOrEqualThanSecond(long firstTime, long secondTime, boolean canEqual) {
        Date firstDate = new Date(firstTime);
        Date secondDate = new Date(secondTime);
        if (secondDate.getYear() > firstDate.getYear()) {
            return true;
        } else if (secondDate.getYear() == firstDate.getYear()) {
            if (secondDate.getMonth() > firstDate.getMonth()) {
                return true;
            } else if (secondDate.getMonth() == firstDate.getMonth()) {
                if (canEqual) {
                    return secondDate.getDate() >= firstDate.getDate();
                } else {
                    return secondDate.getDate() > firstDate.getDate();
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static String format2(long milliseconds) {
        Date today = new Date();
        Date date = new Date(milliseconds);
        //今天
        if (date.getYear() == today.getYear() && date.getMonth() == today.getMonth()
                && date.getDate() == today.getDate()) {
            return formatDateTime(milliseconds, TimeFormat.TIME_HOUR);
        } else { //其他日期
            if (date.getYear() == today.getYear()) {
                return formatDateTime(milliseconds, TimeFormat.TIME_DAY_ALL);
            } else {
                return formatDateTime(milliseconds, TimeFormat.FULL_DAY);
            }

        }
    }

    public static String format(long milliseconds) {
        Date today = new Date();
        Date date = new Date(milliseconds);
        //今天
        if (date.getYear() == today.getYear() && date.getMonth() == today.getMonth()
                && date.getDate() == today.getDate()) {
            return formatDateTime(milliseconds, TimeFormat.TIME_HOUR);
        } else { //其他日期
            return formatDateTime(milliseconds, TimeFormat.TIME_DAY);
        }
    }

    public static String format(Date lastUpdateTime) {
        if (lastUpdateTime != null) {
            return format(lastUpdateTime.getTime());
        }
        return "";
    }

    /**
     * TODO yyyy - MM - (dd + offsetToday) 23:59:59
     * 获取偏移今天 offsetToday 最后一秒的时间
     *
     * @param offsetToday TODO 0:今天: 1: 明天， -1: 昨天, 以此类推
     * @return
     */
    public static Date getDayLastSecond(int offsetToday) {
        Date current = new Date();
        Date tomorroyLastMinute = new Date(0);
        int resultDay = current.getDate() + offsetToday;

        final int year = current.getYear();
        final int month = current.getMonth();
        switch (month) {
            case 0:
            case 2:
            case 4:
            case 6:
            case 7:
            case 9:
            case 11:
                resultDay = DateTimeUtils.makeInRange(resultDay, 1, 31);
                break;

            case 3:
            case 5:
            case 8:
            case 10:
                resultDay = DateTimeUtils.makeInRange(resultDay, 1, 30);
                break;

            case 1:
                if (DateTimeUtils.isRunYear(year)) {
                    resultDay = DateTimeUtils.makeInRange(resultDay, 1, 29);
                } else {
                    resultDay = DateTimeUtils.makeInRange(resultDay, 1, 28);
                }
                break;

            default:
                resultDay = current.getDate();
        }

        tomorroyLastMinute.setYear(year);
        tomorroyLastMinute.setMonth(month);
        tomorroyLastMinute.setDate(resultDay);

        tomorroyLastMinute.setHours(23);
        tomorroyLastMinute.setMinutes(59);
        tomorroyLastMinute.setSeconds(59);
        return tomorroyLastMinute;
    }

    private static int makeInRange(int number, int start, int end) {
        if (number < start) {
            number = start;
        }
        if (number > end) {
            number = end;
        }
        return number;
    }

    /**
     * 是否是闰年
     *
     * @param year 年份
     * @return true 闰年， false 平年
     */
    public static boolean isRunYear(int year) {
        return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }


    /**
     * 今天的显示十分，其他显示月日  dt.getTime() / 1000
     *
     * @param date
     * @return
     */
    public static String getTimeForData(Date date) {
        Date today = new Date();
        //不同年或者月
        if ((date.getYear() != today.getYear()) || date.getMonth() != today.getMonth()) {
            return DateTimeUtils.formatDateTime(date.getTime(), TimeFormat.TIME_DAY);
        }
        //这个月的不同一天
        if (date.getDate() != today.getDate()) {
            return DateTimeUtils.formatDateTime(date.getTime(), TimeFormat.TIME_DAY);
        }
        //今天
        return DateTimeUtils.formatDateTime(date.getTime(), TimeFormat.TIME_HOUR);
    }

    /**
     * 当月的第一天日期
     *
     * @return
     */
    public static String getCurrentMouthStartDay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return formatDateTime(c.getTime(), TimeFormat.FORMAT_YYYYMMDD);
    }

    /**
     * 当月的最后一天日期
     *
     * @return
     */
    public static String getCurrentMouthEndDay() {
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return formatDateTime(ca.getTime(), TimeFormat.FORMAT_YYYYMMDD);
    }

    /**
     * 获取当前时间的年月
     *
     * @return
     */
    public static String getYearAndMouth() {
        Calendar cal = Calendar.getInstance();
        long date = cal.getTimeInMillis();
        String datetime = DateTimeUtils.formatDateTime(date, "yyyy-MM");
        return datetime;
    }

    /**
     * 获取当前时间的年
     *
     * @return
     */
    public static String getYear() {
        Calendar cal = Calendar.getInstance();
        long date = cal.getTimeInMillis();
        String datetime = DateTimeUtils.formatDateTime(date, "yyyy");
        return datetime;
    }

    /**
     * 获得该月第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = createSimpleDateFormat(TimeFormat.FORMAT_YYYYMMDD);
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    /**
     * 获得该月最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = createSimpleDateFormat(TimeFormat.FORMAT_YYYYMMDD);
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2) {
        DateFormat df = createSimpleDateFormat(TimeFormat.FORMAT_YYYYMMDD);
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    public static boolean isDifferenceOneMoth(long startTime, long endTime) {
        Date nextMonthTime = getNextMonthDay(new Date(startTime));
        if (nextMonthTime != null) {
            long nextMonthData = nextMonthTime.getTime();
            if (endTime <= nextMonthData) {
                return true;
            }
        }
        return false;

    }

    public static boolean isDifferenceMoth(long startTime, long endTime, int mothSize) {
        Date nextMonthTime = getNextMonthDay(new Date(startTime), mothSize);
        if (nextMonthTime != null) {
            long nextMonthData = nextMonthTime.getTime();
            if (endTime <= nextMonthData) {
                return true;
            }
        }
        return false;

    }

    public static String getShortTime(String time) {
        Date date = DateTimeUtils.formatDateTime(time, TimeFormat.FULL_DAY);
        return getTime(date.getTime());
    }

    /**
     * 今天的显示十分，其他显示月日
     *
     * @param milliseconds
     * @return
     */
    public static String getTime(long milliseconds) {
        Date today = new Date();
        Date date = new Date(milliseconds);
        //不同年或者月
        if ((date.getYear() != today.getYear()) || date.getMonth() != today.getMonth()) {
            return DateTimeUtils.formatDateTime(milliseconds, TimeFormat.TIME_DAY);
        }
        //这个月的不同一天
        if (date.getDate() != today.getDate()) {
            return DateTimeUtils.formatDateTime(milliseconds, TimeFormat.TIME_DAY);
        }
        //今天
        return DateTimeUtils.formatDateTime(milliseconds, TimeFormat.TIME_HOUR);
    }

    /**
     * 获取距离指定时间下一个月的时间
     *
     * @return
     */
    public static Date getNextMonthDay(Date currentStartTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentStartTime);
        calendar.add(Calendar.MONTH, 1);
        return new Date(calendar.getTimeInMillis());
    }

    public static String parseTimeMillis2DateString(long timeMillis) {
        return parseTimeMillis2DateString(timeMillis, TimeFormat.FULL_DAY);
    }

    public static String parseTimeMillis2DateString(long d, String format) {
        if (d <= 0) {
            return "";
        }
        Date d1 = new Date(d);
        return createSimpleDateFormat(format).format(d1);
    }

    public static Date parseTimeMillis2Date(long timeMillis, String format) {
        return formatDateTime(formatDateTime(timeMillis, format), format);
    }


    /**
     * 获取距离指定时间下个 monthSize 月的时间
     *
     * @param currentStartTime
     * @param monthSize
     * @return
     */
    public static Date getNextMonthDay(Date currentStartTime, int monthSize) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentStartTime);
        calendar.add(Calendar.MONTH, monthSize);
        return new Date(calendar.getTimeInMillis());
    }


    public static enum QUARTER {
        FIRSTQUARTER,
        SECONDQUARTER,
        THIRDQUARTER,
        FORTHQUARTER;

        private QUARTER() {
        }
    }

    public static enum TENDAYS {
        FIRSTTEN,
        MIDTEN,
        LASTTEN;

        private TENDAYS() {
        }
    }

    //获取当前时间
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    //天数
    static long nd = 1000 * 24 * 60 * 60;
    //小时
    static long nh = 1000 * 60 * 60;
    //分钟
    static long nm = 1000 * 60;

    //2个时间差多少小时
    public static long getDistanceHours(long to, long from) {
        long diff = to - from;
        return diff % nd / nh;
    }

    //2个时间差多少分钟
    public static long getDistanceMinutes(long to, long from) {
        long diff = to - from;
        return diff % nd % nh / nm;

    }

    public static long getLastPeroid(int type, Date date){

        Calendar c = Calendar.getInstance();

        if (0 == type){
            //过去1天
            c.setTime(date);
            c.add(Calendar.DATE, -1);
            Date d = c.getTime();
            return d.getTime();
        }else if (1 == type){
            //过去7天
            c.setTime(date);
            c.add(Calendar.DATE, -7);
            Date d = c.getTime();
            return d.getTime();
        }else if (2 == type){
            //过去一月
            c.setTime(date);
            c.add(Calendar.MONTH, -1);
            Date m = c.getTime();
            return m.getTime();
        }else if (3 == type){
            //过去一年
            c.setTime(date);
            c.add(Calendar.YEAR, -1);
            Date y = c.getTime();
            return y.getTime();
        }else if (4 == type){
            //过去二十年
            c.setTime(date);
            c.add(Calendar.YEAR, -20);
            Date y = c.getTime();
            return y.getTime();
        }
        //过去1天
        c.setTime(date);
        c.add(Calendar.DATE, -1);
        Date d = c.getTime();
        return d.getTime();
    }


    //获取本周开始时间
    public static String getWeekStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    //获取本周结束时间
    public static String getWeekEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    //获取本月开始时间
    public static String getMonthStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static String getMonthEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    //获取当天结束时间23：59：59
    private static Date getDayEndTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }



}
